//
//  JDSingleImageViewTableViewCell.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/23.
//

import UIKit

typealias SingleStringBlock = (String?) -> ()

class JDSingleImageViewTableViewCell: UITableViewCell {

    var imageV: UIImageView?
    var finishLoadImageBlock: SingleStringBlock?
    var detailCellHeight: CGFloat = 0
    var imageDownLoadSuccess: Bool = false
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = .commonColor()
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIElements() {
        imageV = UIImageView()
        self.contentView.addSubview(imageV!)
        imageV?.snp.makeConstraints({ (make) in
            make.top.equalTo(self.contentView)
//            make.left.right.equalTo(self.contentView)
            make.left.equalTo(self.contentView).offset(12)
            make.right.equalTo(self.contentView).offset(-12)
            make.bottom.equalTo(self.contentView)
        })
//        imageV?.isHidden = true
        
    }
    
    func updateValueForCell(imageURL: String) {
          
        if imageURL.hasPrefix("http") {
            imageV?.kf.setImage(with: URL(string: imageURL), placeholder: UIImage.init(named: "x"), options: nil, completionHandler: { result in
            })
        } else {
            imageV?.downloadImage(imageURL)
        }
        
        
//        imageV?.kf.setImage(with: URL.init(string: imageURL), placeholder: UIImage.init(named: "placeholder"), options: [], progressBlock: nil, completionHandler: { [unowned self] (result) in
//            self.imageV?.isHidden = false
//            switch result {
//            case .success(let resultValue):
//                if self.imageDownLoadSuccess {
//                    return
//                }
//                let scale = screenWidth/resultValue.image.size.width
//                let height = resultValue.image.size.height*scale
//                self.detailCellHeight = height
//                self.finishLoadImageBlock?("")
//                self.imageDownLoadSuccess = true
//            case .failure(let error):
//                print("图片失败\(error)")
//            }
//
//        })
    }
    
    func updateValueForCell(imageName: String) {
        imageV?.isHidden = false
        imageV?.image = UIImage.init(named: imageName)?.withRenderingMode(.alwaysOriginal)
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
